package com.cloud.salon.client.menber.service.impl;

import java.util.List;

import com.cloud.salon.common.crud.service.impl.CRUDServiceImpl;
import com.cloud.salon.client.menber.repository.StaffRepository;
import com.cloud.salon.client.menber.service.StaffService;
import com.cloud.salon.common.base.response.Res;
import com.cloud.salon.common.base.util.RandomUtil;
import com.cloud.salon.common.base.entity.menber.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * @Description 顾客Service实现类
 * @Author wolf--LZH
 * @CreateDate 2019/4/26 0026 下午 10:00
 */
@Service
public class StaffServiceImpl extends CRUDServiceImpl<Staff, StaffRepository> implements StaffService {

	@Autowired
	private StaffRepository repository;

	public Res<Staff> findByUser(Integer userId) {
		Staff staff = repository.findByUserId(userId);
		if (staff != null)
			return new Res(0, "查询成功", staff);
		else
			return new Res(-3, "查找失败");
	}

	public Res<List<Staff>> findByKey(String key) {
		try{
			return new Res(0,"查找成功", repository.findByKey(key));
		} catch(Exception e) {
			return new Res(-3, "查找失败");
		}
	}
	
	@Override
	public Res<Staff> save(Staff entity) {
		try {
			Staff temp = repository.findByIDNumber(entity.getIdNumber());
			if(temp != null)
				return new Res(-3, "添加失败,已存在该员工");

			// 生成8位随机员工编号和密码
			String tempInfo = RandomUtil.randomNumStrBaseDate(4);
			entity.setNumber(tempInfo);

			return super.save(entity);
		} catch (Exception e) {
			return new Res(-3, "添加失败,请稍后再试");
		}
	}
	
	@Override
	public Res<Staff> update(Staff entity) {
		try {
			Staff temp = (Staff) super.findById(entity.getId()).getData();
			if(temp == null)
				return new Res(-3, "修改失败,无此员工");

			entity.setNumber(temp.getNumber());
			entity.setJoinDate(temp.getJoinDate());
			entity.setIdNumber(temp.getIdNumber());

			return super.update(entity);
		} catch (Exception e) {
			return new Res(-3, "修改失败,请稍后再试");
		}
	}
}
